/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.filter;

import com.apache.api.vo.ResultEntity;
import com.apache.portal.common.oscache.BaseOsCache;
import com.apache.portal.common.oscache.OsCacheOtherManager;
import com.apache.portal.common.util.PortalPubFactory;
import com.apache.portal.task.ScheduledExecutor;
import com.apache.portal.thread.*;
import com.apache.rpc.common.LoadRpcService;
import com.apache.tools.ConfigUtil;
import com.apache.tools.StrUtil;
import com.apache.uct.common.ToolsUtil;
import org.apache.log4j.Logger;

import javax.servlet.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:  启动web工程，加载远程（基础平台）省市区县地区，的数据字典数据到本地的js文件中。
 * web.xml配置文件中配置参数执行此过滤器
 *
 * @author xhe 创建时间：2016-02-22
 */
public class HhreadInitFilter implements Filter {

    private Logger log = Logger.getLogger(HhreadInitFilter.class);

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see javax.servlet.Filter#destroy()
     */
    public void destroy() {

    }

    /**
     * 启动时执行，初始化
     */
    public void init(FilterConfig arg0) throws ServletException {
        //startThread();
        String isFile = StrUtil.doNull(ToolsUtil.getInstance().getLocalByKey("config.properties","read.file.database"),"file");
        ScheduledExecutor.newInstance().setStratWay(isFile);
        if("file".equals(isFile)){
            ScheduledExecutor.newInstance().initStart();
        }
        //加载站点
        //initWebSite();//add0915 先不加载站点
        //CacheHelper.getInstance().getCache("");
    }

    private void startThread() {

        if (ThreadScheduler.newInstance().checkJob("dict")) {//定时生成数据字典
            JobConfig seJobConfig = new JobConfig();
            ISchedulerJob seJob = new CreateDictInfoSchedulerJob();
            seJobConfig.setJob(seJob);
            seJobConfig.setSleepInterval(60 * 60 * 1000);//一个小执行一次
            ThreadScheduler.newInstance().setThreadMap("dict", seJobConfig);
            ThreadScheduler.newInstance().startBykey("dict");
        }
        JobConfig seJobConfig = new JobConfig();
        ISchedulerJob seJob = new CreateParamManagerInfoJob();
        seJobConfig.setJob(seJob);
        seJobConfig.setSleepInterval(20 * 60 * 60 * 1000);//20小时执行一次
        ThreadScheduler.newInstance().setThreadMap("paramManager", seJobConfig);
        ThreadScheduler.newInstance().startBykey("paramManager");
    }

    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
            throws IOException, ServletException {
    }

    private void initWebSite() {
        log.info("开始加载站点：");
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("modelTypes", "s_tWebsite");
        param.put("resultType", "objInfo");
        param.put("sqlKeyId", "objInfo");
        param.put("sysPass", ConfigUtil.getInstance().interfacePass());
        ResultEntity entity = LoadRpcService.service()
                .doServiceClient(PortalPubFactory.getInstance().getBeanId("cms"), "dymicSql", param,
                        PortalPubFactory.getInstance().getRpcInfo("wcm"));
        if (!ToolsUtil.isEmpty(entity.getEntity())) {
            List list = (List) entity.getEntity();
            BaseOsCache oscache = OsCacheOtherManager.getInstance()
                    .getBaseOsCache("sys_website_data_", 60 * 60 * 24 * 30);
            for (int i = 0; i < list.size(); i++) {
                Map map_siteHost = (Map) list.get(i);
                Map map_siteCode = (Map) list.get(i);

                log.info("第[" + (i + 1) + "]域名：" + map_siteHost);
                log.info("第[" + (i + 1) + "]站点：" + map_siteCode);

                String key_siteHost = String.valueOf(map_siteHost.get("siteHost"));
                if (StrUtil.isNotNull(key_siteHost)) {
                    oscache.put(key_siteHost, map_siteHost);
                }

                String key_siteCode = String.valueOf(map_siteCode.get("siteCode"));
                if (StrUtil.isNotNull(key_siteCode)) {
                    oscache.put(key_siteCode, map_siteCode);
                }

            }
        }
    }

}
